<?php


namespace app\model;


use GuzzleHttp\Client;

class Heepay
{
    static public function make($ptype = 'wxapp')
    {
        if (empty($ptype)) {
            $ptype = 'wxapp';
        }
        if ($ptype == 'wxapp') {
            $app_id = Config::getconfig('miniprogram')['app_id'];
        } elseif ($ptype == 'mp') {
            $app_id = Config::getconfig('mp')['app_id'];
        }

        $app = [
            's'=> '微信小程序',
            'n'=>'家里家外智慧养老',
            'id'=>'https://erkaijiazhen.yimaoqi.cn/',
            'is_minipg'=>'1'
        ];
        $app['wx_openid'] = getFans()['openid'];
        $app['wx_sub_appid'] = $app_id;

        $encodeApp = json_encode($app);

        $appStr = iconv('UTF-8','GB18030',$encodeApp);

        return urlencode(base64_encode($appStr));
    }

    static public function sign($data,$key){
        $str = 'version=' . $data['version']  . '&agent_id=' . $data['agent_id'] . '&agent_bill_id=' . $data['agent_bill_id']  . '&agent_bill_time=' . $data['agent_bill_time']  . '&pay_type=' . $data['pay_type']  . '&pay_amt=' . $data['pay_amt']  . '&notify_url=' . $data['notify_url']  . '&return_url=' . $data['return_url']  . '&user_ip='.$data['user_ip'] . '&key=' . $key;
        return md5($str);
    }

    static public function checkSign($data){
        $heepay_settings = Paymethod::gethee_settings();
        return $data['sign'] == md5('result='.$data['result'].'&agent_id='.$data['agent_id'].'&jnet_bill_no='.$data['jnet_bill_no'].'&agent_bill_id='.$data['agent_bill_id'].'&pay_type='.$data['pay_type'].'&pay_amt='.$data['pay_amt'].'&remark='.$data['remark'].'&key='.$heepay_settings['key']);
    }
}
